Position of rightmost set bit using Left Shift(<<)
Follow the steps below to solve the problem:
- Initialize pos with 1
- iterate up to INT_SIZE(Here 32)
- check whether bit is set or not
- if bit is set then break the loop
- else increment the pos.
Below is the implementation of the above approach:
C++
// C++ implementation of above approach #include <iostream> using namespace std; #define INT_SIZE 32 int Right_most_setbit( int num) { if (num == 0) // for num==0 there is zero set bit { return 0; } else { int pos = 1; // counting the position of first set bit for ( int i = 0; i < INT_SIZE; i++) { if (!(num & (1 << i))) pos++; else break ; } return pos; } } int main() { int num = 18; int pos = Right_most_setbit(num); cout << pos << endl; return 0; } // This approach has been contributed by @yashasvi7 |
Java
// Java implementation of above approach public class GFG { static int INT_SIZE = 32 ; static int Right_most_setbit( int num) { int pos = 1 ; // counting the position of first set bit for ( int i = 0 ; i < INT_SIZE; i++) { if ((num & ( 1 << i)) == 0 ) pos++; else break ; } return pos; } // Driver code public static void main(String[] args) { int num = 18 ; int pos = Right_most_setbit(num); System.out.println(pos); } } |
Python3
# Python 3 implementation of above approach INT_SIZE = 32 def Right_most_setbit(num): pos = 1 # counting the position of first set bit for i in range (INT_SIZE): if not (num & ( 1 << i)): pos + = 1 else : break return pos if __name__ = = "__main__" : num = 18 pos = Right_most_setbit(num) print (pos) # This code is contributed by ANKITRAI1 |
C#
// C# implementation of above approach using System; class GFG { static int INT_SIZE = 32; static int Right_most_setbit( int num) { int pos = 1; // counting the position // of first set bit for ( int i = 0; i < INT_SIZE; i++) { if ((num & (1 << i)) == 0) pos++; else break ; } return pos; } // Driver code static public void Main() { int num = 18; int pos = Right_most_setbit(num); Console.WriteLine(pos); } } |
PHP
<?php // PHP implementation of above approach function Right_most_setbit( $num ) { $pos = 1; $INT_SIZE = 32; // counting the position // of first set bit for ( $i = 0; $i < $INT_SIZE ; $i ++) { if (!( $num & (1 << $i ))) $pos ++; else break ; } return $pos ; } // Driver code $num = 18; $pos = Right_most_setbit( $num ); echo $pos ; echo ( "\n" ) // This code is contributed // by Shivi_Aggarwal ?> |
Javascript
<script> // Javascript implementation of above approach let INT_SIZE = 32; function Right_most_setbit(num) { let pos = 1; // Counting the position of first set bit for (let i = 0; i < INT_SIZE; i++) { if ((num & (1 << i)) == 0) pos++; else break ; } return pos; } // Driver code let num = 18; let pos = Right_most_setbit(num); document.write(pos); // This code is contributed by mukesh07 </script> |
Output
2
Time Complexity: O(log2n), Traversing through all the bits of N, where at max there are logN bits.
Auxiliary Space: O(1)
Position of rightmost set bit
Write a one-line function to return the position of the first 1 from right to left, in the binary representation of an Integer.
Examples:
Input: n = 18
Output: 2
Explanation: Binary Representation of 18 is 010010, hence position of first set bit from right is 2.Input: n = 19
Output: 1
Explanation: Binary Representation of 19 is 010011, hence position of first set bit from right is 1.